<template>
  <n-popover trigger="hover">
    <template #trigger>
      <n-badge
        :value="messageList.filter((item) => item.isRead == false).length"
        :offset="[-2, 2]"
      >
        <n-button quaternary circle>
          <template #icon>
            <n-icon size="18">
              <NotificationsOutline></NotificationsOutline>
            </n-icon>
          </template>
        </n-button>
      </n-badge>
    </template>
    <div class="message-box">
      <div class="box-header">
        <div class="title">消息列表</div>
        <n-space>
          <n-button
            @click="handleReadMessage"
            quaternary
            stronger
            type="primary"
            size="small"
            :disabled="
              messageList.filter((item) => item.isRead == false).length == 0
            "
            >全部已读</n-button
          >
          <n-button
            @click="handleDeleteMessage"
            quaternary
            stronger
            type="error"
            size="small"
            :disabled="messageList.length == 0"
            >全部删除</n-button
          >
        </n-space>
      </div>
      <n-divider style="margin: 12px 0"></n-divider>
      <n-scrollbar v-if="messageList.length > 0" style="height: 300px">
        <div
          @click="handleMessageDetail(index)"
          class="message-item"
          v-for="(item, index) in messageList"
        >
          <div class="icon" v-if="item.isRead">
            <n-icon size="22">
              <InformationOutline></InformationOutline>
            </n-icon>
          </div>
          <n-badge v-else dot :offset="[-10, 0]">
            <div class="icon">
              <n-icon size="22">
                <InformationOutline></InformationOutline>
              </n-icon>
            </div>
          </n-badge>

          <div class="message-content">
            <div class="top">{{ item.content }}</div>
            <div class="bottom">
              {{ formatPast(item.createTime) }}
              <span>来自:{{ item["from_user"]?.name }}</span>
            </div>
          </div>
        </div>
      </n-scrollbar>
      <div class="empty" v-else>暂无任何消息</div>
    </div>
  </n-popover>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { formatPast } from "@/utils";
import { NotificationsOutline, InformationOutline } from "@vicons/ionicons5";
import { pageMessage, readMessage, deleteMessage } from "@/api/message";
import { useRouter } from "vue-router";
const router = useRouter();
const messageList = ref([]);
const handleInfoSelect = () => {};
const handleReadMessage = (id = 0) => {
  readMessage({
    ids:
      id != 0
        ? [id]
        : messageList.value
            .filter((item) => item.isRead == false)
            .map((item) => item["id"]),
  })
    .then((res) => {
      if (id != 0) {
        messageList.value.forEach((item) => (item.isRead = true));
      }
    })
    .catch((e) => {
      console.log(e);
    });
};
const handleDeleteMessage = () => {
  deleteMessage({
    ids: messageList.value.map((item) => item["id"]),
  }).then((res) => {
    messageList.value = [];
  });
};
const handleMessageDetail = (index: number) => {
  const message = messageList.value[index];
  handleReadMessage(message["id"]);
  message.isRead = true;

  if (message.url) {
    router.push(message.url);
  }
};
onMounted(() => {
  pageMessage({ page: 1, size: 20 }).then((res: any) => {
    console.log(res);
    messageList.value = res.data.list;
  });
});
</script>

<style lang="scss" scoped>
.message-box {
  .box-header {
    width: 300px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .message-item {
    margin: 6px 0;
    display: flex;
    align-items: center;
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    user-select: none;
    transition: background-color 0.3s ease;
    .icon {
      margin-right: 12px;
      background-color: #f2f3f5;
      width: 30px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 4px;
    }
    .top {
      // font-weight: bold;
    }
    .bottom {
      font-size: 12px;
      color: grey;
      margin-top: 6px;
    }
  }

  .message-item:hover {
    background-color: rgba(24, 160, 88, 0.1);
  }
  .message-item:active {
    background-color: rgba(24, 160, 88, 0.3);
  }
}
.dark .message-item:hover {
  background-color: rgba(99, 226, 183, 0.15);
}
.dark .message-item:active {
  background-color: rgba(99, 226, 183, 0.3);
}
.dark .icon {
  background-color: #333 !important;
}
.empty {
  display: flex;
  height: 80px;
  justify-content: center;
  align-items: center;
}
</style>
